package cmcc.dbi.execJdbcUtil;

import cmcc.dbi.dto.DbQueryDTO;
import cmcc.dbi.entity.InspectionItemRecord;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class getCmpResJdbcUtil implements JdbcUtil{

    @Override
    public JSONArray getTargetResult(List<InspectionItemRecord> scriptsLists, DbQueryDTO dbQueryDTO) {
        JSONArray queryAllResult = new JSONArray();
        //区分巡检项的巡检库范围---为0则是默认库
        List<InspectionItemRecord> scriptsLists4Thedb = scriptsLists.stream().filter(item -> item.getInspectedObject() == 0).collect(Collectors.toList());
        //为1则为所有库
        List<InspectionItemRecord> scriptsLists4Alldb = scriptsLists.stream().filter(item -> item.getInspectedObject() == 1).collect(Collectors.toList());

        //调用默认库巡检
        JSONArray queryAllResult4Thedb = null;
        try {
            if (!scriptsLists4Thedb.isEmpty()) {
                getRes4TheDbUtil getRes4TheDbUtil = new getRes4TheDbUtil();
                queryAllResult4Thedb = getRes4TheDbUtil.getTargetThedbResult(scriptsLists4Thedb, dbQueryDTO);
            }
        } catch (Exception e) {
            queryAllResult4Thedb = makeErrorResult(scriptsLists4Thedb, e.getMessage());
        } finally {
            if (queryAllResult4Thedb != null){
                queryAllResult.addAll(queryAllResult4Thedb);
            }
        }

        //调用全部库巡检
        JSONArray queryAllResult4Alldb = null;
        try {
            if (!scriptsLists4Alldb.isEmpty()) {
                //调用所有库巡检
                getRes4AllDbUtil getRes4AllDbUtil = new getRes4AllDbUtil();
                queryAllResult4Alldb = getRes4AllDbUtil.getTargetAlldbResult(scriptsLists4Alldb, dbQueryDTO);
            }
        } catch (Exception e) {
            queryAllResult4Alldb = makeErrorResult(scriptsLists4Alldb, e.getMessage());
        } finally {
            if(queryAllResult4Alldb != null){
                queryAllResult.addAll(queryAllResult4Alldb);
            }
        }
        return queryAllResult;
    }

    private JSONArray makeErrorResult(List<InspectionItemRecord> scriptsLists,String e) {
        JSONArray queryAllResult = new JSONArray();
        log.info("sql巡检出错，makeErrorRes : "+e);
        for (InspectionItemRecord scriptsList : scriptsLists) {
            // 保存单个巡检项对应的结果
            List<Map<String, Object>> lisTmp = new ArrayList<>();
            // 获取当前巡检项的脚本
            JSONObject querySingleResult = new JSONObject();
            Map<String, Object> tmp = new HashMap<>();
            tmp.put("error_info", String.valueOf(e));
            lisTmp.add(tmp);
            querySingleResult.put("item_id", scriptsList.getItemId());
            querySingleResult.put("jdbc_result", lisTmp);
            queryAllResult.add(querySingleResult);
        }
        return  queryAllResult;
    }


}
